<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 **/

namespace Admin\Controller;
use Vendor\Tree;

class LicenseTypeController extends ComController
{

    public function add()
    {
        $where['d_id'] = $this->USER['uid'];
        $license = M('license')->where($where)->field('id,type')->select();
        $this->assign('license', $license);//导航
        $this->display('form');
    }

    public function index()
    {
        $model = new \Think\Model();
        $mysql = $model->query("select VERSION() as mysql");
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $license = M('license_type');
        $prefix = C('DB_PREFIX');
        //获得当前登录驾校id
        $d_id = $this->USER['uid'];
        $where = '1 = 1 ';
        if($d_id){
            $where .= "and {$prefix}license_type.d_id = '$d_id' and {$prefix}license_type.d_id > '1'";
        }

        $pagesize = 6; //每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $where1['d_id']=$d_id;
        $count = $license->where($where1)->count();
        $list = $license->where($where)
            ->field("{$prefix}license_type.*,{$prefix}member.name as school_name")
            ->join("{$prefix}member ON {$prefix}member.uid = {$prefix}license_type.d_id")
            ->limit($offset . ',' . $pagesize)
            ->order('id desc')
            ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('mysql', $mysql[0]['mysql']);
        $this->assign('nav', array('', '', ''));//导航
        $this->display();
    }

    public function del()
    {
        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if ($ids) {
            if (is_array($ids)) {
                $aids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }

            if($comments = M("license_type")->where($map)->delete()){
                $this->success('驾照类型删除成功！');
            }else{
                $this->error('参数错误，请联系您的技术支持！');
            }
        } else {
            $this->error('您没有选择要删除的驾照类型！');
        }
    }

    public function edit()
    {
        $id = I('get.id');
        $var = M('license_type')->where('id=' . $id)->find();
        if ($var) {

            $license = M('license')->field('id,type')->select();
            $this->assign('license', $license);//导航

            $this->assign('var', $var);
        } else {
            $this->error('参数错误！');
        }
        $this->assign('id',$id);
        $this->display('form');
    }

    public function update()
    {
        $type=I('post.type');
        if(empty($type)){
            $this->error('驾照类型不能为空!');
        }
        $varname = I('post.varname');
        $where['id']=I('post.id');
        $data['d_id'] = $this->USER['uid'];
        $data['type'] = I('post.type');
        $data['notes'] = I('post.notes');
        $type = I('post.type');

        if ($varname == '') {

            $d_id = $this->USER['uid'];
            $where1['d_id']=$d_id;
            $where1['type']=$type;
            $type = M('license_type')->where($where1)->select();
            if($type){
                $this->error('驾照类型已存在!');
            }

            M('license_type')->data($data)->add();
            $this->success('恭喜，操作成功！', U('index'));
        } else {
            $d_id = $this->USER['uid'];
            $newType = I('post.type');
            $where['id']=I('post.id');
            $where['d_id']=$d_id;
            $license = M('license_type')->where($where)->select();

            if($newType !== $license[0]['type']){
                $where1['d_id']=$d_id;
                $where1['type']=$newType;
                $type = M('license_type')->where($where1)->select();
                if($type){
                    $this->error('驾照类型已存在!');
                }else{
                    M('license_type')->data($data)->where($where)->save();
                    $this->success('恭喜，操作成功！', U('index'));
                }
            }elseif($newType == $license[0]['type']){
                M('license_type')->data($data)->where($where)->save();
                $this->success('恭喜，操作成功！', U('index'));
            }


        }

    }

}
